/*
function scanMessages() {
	console.log("start request");
	var conn = new Ext.data.Connection();
	conn.request({
		timeout: 3600000,
	    url: getMainUrl(),
	    method: 'POST',
	    params: {action: "messenger", op: "scan"},
	    success: function(response) {
        	obj = Ext.util.JSON.decode(response.responseText);
			console.log(response);
	    },
		failure: function() {
	         //Ext.Msg.alert('Status', 'Unable to show history at this time. Please try again later.');
		},
		callback: function ( options, success, response ) {
			console.log("callback");
			scanMessages();
		}		
	});	
}
*/
MyDesktop.Messenger = Ext.extend(Ext.app.Module, {
	id: 'messenger-win',
	text: _('Messenger'),
	iconCls:'messenger',
	windowId : 'messenger',	
    init : function(){
        this.launcher = {
			text: this.text,
			iconCls: this.iconCls,
			windowId : this.windowId,	
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(src){
        var desktop = this.app.getDesktop();
        var winname = this.windowId;
        var win = desktop.getWindow(winname);

        if(!win){
        	var lMask;
        	var tree = new Ext.tree.TreePanel({
                        id:'im-tree',
                        title: _('Active Users'),
                        loader: new Ext.tree.TreeLoader({
                        	url: getMainUrl(),
                        	baseParams:{action: 'messenger'},
				            listeners: {
				                'beforeload': function() {
				                    	lMask = new Ext.LoadMask(tree.getEl(), {msg: _('Loading...'), removeMask: true});
				                    	lMask.show();
				                },
				                'load': function() {
				                	lMask.hide();
				                },
				                'loadexception': function() {
				                	lMask.hide();
				                }				                
				            }
                        }),
                        rootVisible:false,
                        lines:false,
                        autoScroll:true,
                        //collapsible: false,
				        root: new Ext.tree.AsyncTreeNode({
				            id: 'id'
				        }),
				        selModel: new Ext.tree.MultiSelectionModel()                        
                        /*tools:[{
                            id:'refresh',
                            on:{
                                click: function(){
                                    var tree = Ext.getCmp('im-tree');
                                    tree.body.mask('Loading', 'x-mask-loading');
                                    tree.root.reload();
                                    tree.root.collapse(true, false);
                                    setTimeout(function(){ // mimic a server call
                                        tree.body.unmask();
                                        tree.root.expand(true, true);
                                    }, 1000);
                                }
                            }
                        }],*/
			});
        	
        	tree.on({
				'dblclick': function(node, ev) {
					  	var util = new Verbum.Utilities();
						util.messageForm({
							ids: '["'+node.id+'"]'
						});
					}
			});
        	var helpMsg = String.format(_('<ul><li>To send a message to a single user, double click on its name.</li><li>To send a message to a group of users, select them while pressing Ctrl key, and then press {0} button.</li></ul>'),_('Send PM'));
            win = desktop.createWindow({
            	id: winname,
                title: this.text,
                width:250,
                height:400,
                iconCls: this.iconCls,
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                tbar:[{
					text: _('Send PM'),
					tooltip: _('Send a message'),
					iconCls: 'mail',
					handler: function () {
						var selections = tree.selModel.getSelectedNodes();
						if (selections.length >0) {
						  	var prez = [];
						  	for(i = 0; i< selections.length; i++){
								prez.push(selections[i].id);
						  	}
						  	var encoded_array = Ext.encode(prez);
						  	var util = new Verbum.Utilities();
							util.messageForm({
								ids: encoded_array
							});
						} else {
							Ext.Msg.message(_('You have to select an User.'));
						}								
					}
                },'->',
                {
                	tooltip: _('Help'),
                	iconCls: 'help',
                	handler: function() {
	            		var hwin = Ext.get('m-help-win');
	            		if (!hwin) {
					        hwin = desktop.createWindow({
				                id: 'm-help-win',
				                title: _('Help'),
				                width:400,
				                height:150,
				                iconCls: 'help',
				                shim:false,
				                animCollapse:false,
				                border:false,
				                constrainHeader:true,
				                maximizable: false,
				                rezisable: false,
				                layout: 'fit',
				                items: new Ext.Panel({
			                        items: [{
		    	                        html : '<br/>'+helpMsg,
		        	                    border:false
		            	            }
		            	            ]})
				           	});
	            		}
			           	hwin.show();      		
                	}
                
                }],
                layout:'accordion',
                border:false,
                layoutConfig: {
                    animate:false,
                    collapsible: false,
                },
                items: tree
            });
        }

/*                      title: _('Other Teams'),
                        html:'<p>Something useful would be in here.</p>',
                        autoScroll:true
                    },{
                        title: _('Settings'),
                        html : '<p>Something useful would be in here.</p>'
                    }*/
                    
        win.show();
        
    },
	display : function(){
        this.createWindow();
	}
});

//scanMessages();